component{
	this.entityObj = "category";

	include "../../inc/lib.cfm";
	public any function getTree(rc){
		var result = {};
		var hql = "from category where categoryid is null and categorytypeid=#rc.categorytypeid# order by categoryid asc,seq asc";
		var result = ormExecuteQuery(hql,false);
		return result;
	}

	public any function getlistbytarget(rc){

		if(structKeyExists(rc, "id"))
		{
			query name="q"{
				echo("SELECT id,text FROM category where categoryid = ");
				queryparam cfsqltype="cf_sql_integer" value="#rc.id#";
			}

		}else{
			query name="q"{
				echo("SELECT id,text FROM category where level = 2");
			}

		}


		var tmp = {};
		var result = [];
		for(var i=1;i<=q.recordcount;i++){
			tmp[i]['id'] = q['id'][i];
			tmp[i]['text']= q['text'][i];
			result[i] = tmp[i];
		}
		return result;
	}


	public any function listtree(rc){

		if(structKeyExists(rc, "id"))
		{
			var id = rc.id;
		}else{
			var id = 0;
		}

		var result = {};

		if(id == 0){
			var hql = " from categoryflat where categoryid is null and categorytypeid=#rc.categorytypeid# order by categoryid asc,seq asc";
			var result = ormExecuteQuery(hql,false);
		}else{
			var hql = " from categoryflat where categoryid = #id# and categorytypeid=#rc.categorytypeid# order by categoryid asc,seq asc";
			var result = ormExecuteQuery(hql,false);
		}
		for(var i=1;i<=arraylen(result);i++){
			if(!isnull(result[i].getcategoryid())){
				result[i]["_parentId"] = result[i].getCategoryid();
			}else{
				result[i]["_parentId"] = 0;
			}

			var ctotalhql = "select count(id) as ctotal from categoryflat where categoryid=#result[i].getid()#";
			var rtotal = ormExecuteQuery(ctotalhql,true);

			if(rtotal>0){
				result[i]["state"] = 'closed';
			}else{
				result[i]["state"] = 'open';
			}
		}

		return result;
	}


	public any function list(rc){

		if(structKeyExists(rc, "id"))
		{
			var id = rc.id;
		}else{
			var id = 0;
		}

		var result = {};

		if(id == 0){
			var hql = " from categoryflat where categoryid is null and categorytypeid=#rc.categorytypeid# order by categoryid asc,seq asc";
			var result["rows"] = ormExecuteQuery(hql,false);
			var totalhql = "select count(id) as total from  categoryflat where categoryid is null and categorytypeid=#rc.categorytypeid#";
			var result["total"] = ormExecuteQuery(totalhql,true);
		}else{
			var hql = " from categoryflat where categoryid = #id# and categorytypeid=#rc.categorytypeid# order by categoryid asc,seq asc";
			var result["rows"] = ormExecuteQuery(hql,false);
			var totalhql = "select count(id) as total from  categoryflat where categoryid = #id# and categorytypeid=#rc.categorytypeid#";
			var result["total"] = ormExecuteQuery(totalhql,true);
		}
		/*
		newResult = [];
		for(var i=1;i<=arraylen(result.rows);i++){
			var tmpResult = {};
			tmpResult["url"] = result.rows[i].getUrl();
			tmpResult["text"] = result.rows[i].getText();
			tmpResult["id"] = result.rows[i].getId();
			tmpResult["description"] = result.rows[i].getDescription();
			tmpResult["seq"] = result.rows[i].getSeq();

			if(!isnull(result.rows[i].getParent())){
				tmpResult["_parentId"] = result.rows[i].getParent().getID();
			}else{
				tmpResult["_parentId"] = 0;
			}

			tmpResult["categorytype"] = result.rows[i].getCategorytype().getName();
			newResult[i] = tmpResult;
		}
		result["rows"] = newResult;
		*/
		for(var i=1;i<=arraylen(result.rows);i++){
			if(!isnull(result.rows[i].getcategoryid())){
				result.rows[i]["_parentId"] = result.rows[i].getCategoryid();
			}else{
				result.rows[i]["_parentId"] = 0;
			}

			var ctotalhql = "select count(id) as ctotal from categoryflat where categoryid=#result.rows[i].getid()#";
			var rtotal = ormExecuteQuery(ctotalhql,true);

			if(rtotal>0){
				result.rows[i]["state"] = 'closed';
			}else{
				result.rows[i]["state"] = 'open';
			}
		}
		return result;
	}

	//获取categorytype对象
	public any function getCategorytype(rc){
		var result = {};
		var hql = " from categorytype ";
		var result = ormExecuteQuery(hql,false);
		return result;
	}

	//获取操作
	public any function get(numeric id){
		result = entityLoadByPk("categoryflat", arguments.id);
		return result;
	}

	public any function getall(rc){
		var hql = " from category where categorytypeid=#rc.categorytypeid# order by categoryid asc,seq asc";
		var result = ormExecuteQuery(hql,false);
		return result;
	}

	//新增操作，调用的是categoryflat实体对象
	public any function save(any rc){
		transaction {
            var q = entityNew("categoryflat");
			include "../../inc/saveandupdate.cfm";
        }
        return q;
	}

	//删除操作
	public any function delete(numeric id){
		var q = entityLoadByPk("category", arguments.id);
		entityDelete(q);
		transactionCommit();
		return q;
	}

	//修改操作
	public any function update(any rc){
		transaction {
            var q = entityLoadByPk("categoryflat", rc.id);
			include "../../inc/saveandupdate.cfm";
        }
        return q;
	}

}